﻿#include "dialog.h"
#include "ui_dialog.h"
#include <opencv2/opencv.hpp>
#include <QElapsedTimer>
#include <QDebug>
#include <QFileDialog>
//#include "opencvBuildingBlock/TemplateMatch.h"
#include "opencvBuildingBlock/PyramidTemplateMatch.h"
#include <qtOpenCV/cvQDebug.h>
//#include "test.h"
#include <omp.h>
Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::Dialog)
{
    ui->setupUi(this);
    setWindowTitle("Pyramid Template Match");

    m_pPyramidMatcher = new Qly::PyramidTemplateMatch(5);
    m_pPyramidMatcher->setSubPixMode(false);
    //speedTest04();
    //article05();
    //test05();

    box1 = new PictureBox(PictureBox::AUTO_ZOOM, ui->widgetImage);
    QVBoxLayout *vbox = new QVBoxLayout;
    vbox->addWidget(box1);
    ui->widgetImage->setLayout(vbox);


    box2 = new PictureBox(PictureBox::AUTO_ZOOM, ui->widgetModel);
    QVBoxLayout *vbox2 = new QVBoxLayout;
    vbox2->addWidget(box2);
    ui->widgetModel->setLayout(vbox2);

    m_path = qApp->applicationDirPath();
}

Dialog::~Dialog()
{
    delete ui;
}


void Dialog::on_pushButtonOpenTemplate_clicked()
{
    QString fileName = QFileDialog::getOpenFileName(this,
                                                    tr("Open Image"), m_path, tr("Image Files (*.png *.jpg *.bmp)"));
    QElapsedTimer t;
    t.start();
    m_templateImage.load(fileName);
    //m_templateImage = m_templateImage.copy();
    QImage img = m_templateImage.convertToFormat(QImage::Format_Grayscale8);

    model = QImage2cvMat(img, true);

    // cv::Mat mat  = imageRotateAutoSize(model, 30);

    // cv::RotatedRect rrt;
    // rrt.center.x = 1000;
    // rrt.center.y= 1000;
    // rrt.size.width = 500;
    // rrt.size.height = 300;
    // rrt.angle = 30;
    // Qly::drawRotatedRect(mat, rrt, cv::Scalar(255, 255, 255), 3);
    // cv::imshow("30", mat);
    box2->setImage(m_templateImage);

    double angleStart = ui->lineEditAngleStart->text().toDouble();
    double angleEnd = ui->lineEditAngleEnd->text().toDouble();
    int k = ui->lineEditAngleDet->text().toInt();
    m_pPyramidMatcher->setModelByAngleCount(model, angleStart, angleEnd, k);

    double v = t.elapsed() / 1000.0;

    ui->labelLoadModelTime->setText(QString("%1 s").arg(v));

    m_path = fileName;
}

void Dialog::on_pushButtonOpenImage_clicked()
{
    QString fileName = QFileDialog::getOpenFileName(this,
                                                    tr("Open Image"), m_path, tr("Image Files (*.png *.jpg *.bmp)"));

    m_testImage.load(fileName);
    m_testImage = m_testImage.convertToFormat(QImage::Format_Grayscale8, Qt::AutoColor);
    testImg = QImage2cvMat(m_testImage, true);
    box1->setImage(m_testImage);

    m_path = fileName;
}

void Dialog::on_pushButtonRunMatch_clicked()
{

    cv::RotatedRect rrt;
    double score;

    if(ui->checkBoxMultiThread->isChecked())
    {
        omp_set_num_threads(2);
    }
    else
    {
        omp_set_num_threads(1);
    }
    QElapsedTimer t;
    t.start();
   //bool ret = m_rotateMatcher.fastRotatedMatch2(testImg, 4, rrt, score);

    bool ret = m_pPyramidMatcher->match(testImg, rrt, score);
    if(ret != true) return;

    QPointF p[4];
    cv::Point2f vertices[4];

    rrt.angle = -rrt.angle;
    rrt.points(vertices);
    rrt.angle = -rrt.angle;
    for (int i = 0; i < 4; i++)
    {
        p[i] = QPointF(vertices[i].x, vertices[i].y);
    }
    ui->labelScore->setNum(score);

    ui->labelX->setNum(rrt.center.x);
    ui->labelY->setNum(rrt.center.y);
    ui->labeltheta->setNum(rrt.angle);
    box1->setRotatedRect(p);
    box1->update();

    double v = t.elapsed() / 1000.0;
    ui->labelComputeTime->setText(QString("%1 s").arg(v));


}
